package com.space.school.controller;

import com.space.school.domain.ProductCategory;
import com.space.school.domain.User;
import com.space.school.service.impl.ProductListServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name ="productListOperate" ,value = "/productListOperate")
public class ProductListOperateServlet extends HttpServlet {
    private ProductListServiceImpl productListService = new ProductListServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String category_id = req.getParameter("category_id");
        String operate = req.getParameter("operate");
//        删除操作
        if(operate.equals("del")){
            productListService.del(Integer.parseInt(category_id));

            resp.sendRedirect("/productInfoByPageServlet");
        }else if(operate.equals("update")){
//          修改操作
            ProductCategory productCategory=productListService.find(Integer.parseInt(category_id));
            if(productCategory!=null){
                req.setAttribute("productCategory",productCategory);
                req.getRequestDispatcher("updateProductList.jsp").forward(req,resp);
            }
        }else if(operate.equals("add")){
//            添加操作
            req.getRequestDispatcher("addProductList.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
//        提取类目id 名字 编号
        String category_id = req.getParameter("category_id");
        String category_name=req.getParameter("category_name");
        String category_type = req.getParameter("category_type");
//        提取类目修改的新 名字 编号
        String category_new_name=req.getParameter("category_new_name");
        String new_category_type = req.getParameter("new_category_type");

        if(category_id==null){
//            新增操作


        }else {
//            修改操作
            ProductCategory productCategory=new ProductCategory();
            productCategory.setCategory_id(Integer.parseInt(category_id));
            productCategory.setCategory_name(category_new_name);
            productCategory.setCategory_type(Integer.parseInt(new_category_type));
            productListService.update(productCategory);
            resp.sendRedirect("/productInfoByPageServlet");
        }
    }
}
